package com.ruoyi.common.enums.business;


import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum CouponStatusEnum {

    UNUSED(1, "未使用"),
    USED(2, "已使用"),
    EXPIRED(3, "已过期"),
    INVALIDATED(4, "已作废");

    private final Integer code;
    private final String desc;

    private static final Map<Integer, CouponStatusEnum> ENUM_MAP;

    static {
        CouponStatusEnum[] values = values();
        Map<Integer, CouponStatusEnum> map = new HashMap<>(values.length);
        for (CouponStatusEnum value : values) {
            map.put(value.code, value);
        }
        ENUM_MAP = Collections.unmodifiableMap(map);
    }

    /**
     * 根据code查找枚举
     */
    public static CouponStatusEnum getEnumByCode(Integer code) {
        CouponStatusEnum couponStatusEnum = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(couponStatusEnum)) {
            throw new RuntimeException("无效的兑换券状态编码: " + code);
        }
        return couponStatusEnum;
    }

    /**
     * 获取描述
     */
    public static String getDescByCode(Integer code) {
        CouponStatusEnum couponStatusEnum = ENUM_MAP.get(code);
        return ObjectUtil.isEmpty(couponStatusEnum) ? "未知状态" : couponStatusEnum.getDesc();
    }

    /**
     * 判断是否为可用状态
     */
    public static boolean isAvailable(Integer code) {
        return UNUSED.getCode().equals(code);
    }



}
